package org.ysh.vo;

import lombok.Data;
import org.ysh.domain.Department;

import java.util.ArrayList;
import java.util.List;

@Data
public class TreeNodeVo {

    private Integer id;

    private Integer parentId;

    private String name;

    private String icon = "&#xe6af;";

    public TreeNodeVo(){}

    public TreeNodeVo(Integer id, Integer parentId, String name) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }

    private List<TreeNodeVo> children;

    public static List<TreeNodeVo> buildTree(List<Department> departments){
        List<TreeNodeVo> vos = new ArrayList<>();
        if(departments==null || departments.isEmpty()){
            return vos;
        }

        return findChildren(-1,departments);
    }

    private static List<TreeNodeVo> findChildren(Integer id , List<Department> departments) {
        List<TreeNodeVo> children = new ArrayList<>();
        for(Department department : departments){
            if(department.getParentId().equals(id)){
                TreeNodeVo fondsTreeNodeVo = new TreeNodeVo(department.getId(),department.getParentId(),department.getDepartmentName());
                children.add(fondsTreeNodeVo);
                //递归查找子节点
                fondsTreeNodeVo.setChildren(findChildren(department.getId(), departments));
            }
        }
        return children.isEmpty() ? null : children;
    }
}
